package utils;

import com.google.gson.JsonObject;
import constants.Client;
import constants.Configurations;
import constants.Default;
import constants.session.Session;
import constants.system.MemberShip;
import duppentity.DuppException;
import play.Logger;
import play.cache.Cache;
import play.mvc.Http;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class DuppEntityUtil {

    /**
     * 获取ds数据
     *
     * @param service
     * @param kv
     * @return
     */
    public static JsonObject getEntityWithRaw(String caller,String service, Map<String, Object> kv) {
        JsonObject data = null;
        try {

            //每个接口都传递client
            HashMap<String, Object> subKv=new HashMap<>();
            //循环端提供的参数
            Client client=new Client();
            if (Utils.isNullOrEmpty(caller)) {
                caller = client.caller;
            }
            if (caller.equals(Default.callerType.two)){
                subKv.put("caller",Configurations.duppSaccDataClientCaller);
            }else {
                subKv.put("caller", caller);
            }
            Http.Cookie cookie = Http.Request.current().cookies.get("__UUID");
            client.uuid = cookie.value;
            subKv.put("uuid",client.uuid);

            if(kv==null) {
                kv=new HashMap<>();
            }
            kv.put("client",subKv);
            //如果@Param注解里面有"userId"参数或列表筛选时参数有"userId"时，就不传存在缓存cache里面的"userId"
            if (!((kv.containsKey("userId") || service.contains("getList")))) {
                kv.put("userId", MemberShip.current().getId());
            }
            kv.put("sessionId", MemberShip.current().getSessionId());  //服务端返回的sessionId

            data = DuppUtil.sendAndGetRaw(service,caller, kv, false);
        } catch (Exception ex) {
            if(ex instanceof DuppException){
                throw ex;
            }
            Logger.error("DuppEntityUtil: getEntity failed, %s, %s,%s", service, kv.toString(),ex);
        }
        return data;
    }
}
